package com.fz.us.oa.web.controller.com;

import com.fz.us.admin.base.bean.BaseEnumManager;
import com.fz.us.admin.base.bean.jqgrid.JqGridFilter;
import com.fz.us.admin.base.bean.jqgrid.JqGridPager;
import com.fz.us.admin.base.utils.EntityConfig;
import com.fz.us.admin.base.utils.ModuleConfig;
import com.fz.us.admin.company.entity.User;
import com.fz.us.oa.core.entity.com.ModulePower;
import com.fz.us.oa.core.entity.com.StartExam;
import com.fz.us.oa.core.service.com.ModulePowerService;
import com.fz.us.oa.core.service.com.StartExamService;
import com.fz.us.oa.web.controller.BaseController;
import com.fz.us.oa.web.shiro.bind.annotation.CurrentUser;
import com.fz.us.oa.web.spring.bind.annotation.RequestJsonParam;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * Created by dzw on 2017/12/14.
 */
@Controller
@RequestMapping(value = "/com/startExam")
public class StartExamController extends BaseController {
    private static final long serialVersionUID = -1475724659764510297L;

    @Autowired
    private ModulePowerService modulePowerService;
    @Autowired
    private StartExamService startExamService;

    private static final EntityConfig entityConfig = new EntityConfig();
    private static final ModuleConfig moduleConfig = new ModuleConfig();

    /**
     * 发起审批页面
     */
    @RequestMapping(value = "toCreateExam", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
    public String toCreateExam(@CurrentUser User user, Model model){
        ModulePower modulePower=modulePowerService.getModulePowerPassByUser(user);
        if(modulePower!=null){
            List<String> moduleNameList= Arrays.asList(modulePower.getModuleName().split(","));
            model.addAttribute("moduleNameList", moduleNameList);
        }
        return "/main/menu/createExam";
    }

    /**
     * 获取当前登录人员的发起审批的表单
     */
    @RequestMapping(value = "getStartExamList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Object getStartExamList(@CurrentUser User user){
        Map<String, Object> params=new HashMap<String, Object>();
        params.put("user", user);
        List<StartExam> startExamList=startExamService.getList(params);
        Map<String, Object> rMap;
        List<Map<String, Object>> data=new ArrayList<Map<String, Object>>();
        ModulePower modulePower=modulePowerService.getModulePowerPassByUser(user);
        List<String> formKeyList=new ArrayList<String>();
        if(modulePower!=null){
            formKeyList= Arrays.asList(modulePower.getEntityName().split(","));
        }

        for(StartExam startExam:startExamList){
            if(formKeyList.contains(startExam.getFormKey())){
                rMap=new HashMap<String, Object>();
                rMap.put("formKey", startExam.getFormKey());
                rMap.put("formName", startExam.getFormName());
                rMap.put("moduleName", startExam.getModuleName());
                data.add(rMap);
            }
        }
        return data;
    }

    /**
     * 选择表单页面
     */
    @RequestMapping(value = "chooseForm", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
    public String chooseForm(@CurrentUser User user, Model model,String cKeys, String moduleName) {

        List<Map<String,Object>> mapList = entityConfig.getAllProperties();
        Map<String,Object> rmap;
        model.addAttribute("cKeys", cKeys);
        model.addAttribute("moduleName", moduleName);
        if (StringUtils.isNotBlank(cKeys)) {
            ArrayList<Map<String,Object>> sFormMap = new ArrayList<Map<String,Object>>();
            for (String pid : cKeys.split(",")) {
                for(Map map:mapList){
                    if(StringUtils.equals(pid, map.get("key").toString())){
                        rmap = Maps.newHashMap();
                        rmap.put("key", map.get("key").toString());
                        rmap.put("value", map.get("value").toString());
                        sFormMap.add(rmap);
                        break;
                    }
                }
            }
            model.addAttribute("sFormMap", sFormMap);
        }
        return "/main/menu/createExam_dialog";
    }

    /**
     * 获取表单列表
     */
    @RequestMapping(value = "getFormList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> getFormList(@CurrentUser User user,
                                    JqGridPager pager,
                                    String moduleName,
                                    @RequestJsonParam(value = "filters", required = false) JqGridFilter filters) {
        List<Map<String,Object>> entityList = entityConfig.getAllProperties();
        List<Map<String,Object>> moduleList = moduleConfig.getAllProperties();

        List<String> entityNameList=new ArrayList<String>();
        for(Map map:moduleList){
            if(moduleName.equals(map.get("value").toString())){
                entityNameList.add(map.get("key").toString());
            }
        }

        ArrayList<Map<String, Object>> dataRows = new ArrayList<Map<String, Object>>();
        Map<String, Object> data = new HashMap<String, Object>();
        Map<String,Object> rmap;

        List<String> entityNamePassList=new ArrayList<String>();
        ModulePower modulePower=modulePowerService.getModulePowerPassByUser(user);
        if(modulePower!=null){
            entityNamePassList=Arrays.asList(modulePower.getEntityName().split(","));
        }

        for(Map map:entityList){
            if(entityNameList.contains(map.get("key").toString()) && entityNamePassList.contains(map.get("key").toString())){
                rmap = Maps.newHashMap();
                rmap.put("id", map.get("key").toString());
                rmap.put("name", map.get("value").toString());
                dataRows.add(rmap);
            }
        }
        data.put("dataRows", dataRows);

        return data;
    }

    /**
     * 保存发起审批数据
     */
    @RequestMapping(value = "startExamEdit", method = RequestMethod.POST)
    public Object startExamEdit(@CurrentUser User user, String moduleName, String formName, String formKey){
        startExamService.delByModuleAndUser(moduleName, user);
        if(StringUtils.isNotEmpty(formKey)){
            String formNameArr[]=formName.split(",");
            String formKeyArr[]=formKey.split(",");
            for(int i=0;i<formNameArr.length;i++){
                StartExam startExam=new StartExam();
                startExam.setCreater(user);
                startExam.setCompany(user.getCompany());
                startExam.setState(BaseEnumManager.StateEnum.Enable);
                startExam.setUser(user);
                startExam.setModuleName(moduleName);
                startExam.setFormKey(formKeyArr[i]);
                startExam.setFormName(formNameArr[i]);
                startExamService.save(startExam);
            }
        }
        return ajaxJson(resultService.success("保存成功"));
    }

}
